package com.qlmjx.sevalopos.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.qlmjx.sevalopos.Bean.ErW;
import com.qlmjx.sevalopos.Bean.ManageOrderInfo;
import com.qlmjx.sevalopos.Bean.ManagePayInfo;
import com.qlmjx.sevalopos.Bean.OrederBean;
import com.qlmjx.sevalopos.R;
import com.qlmjx.sevalopos.adapter.OrderPayAdapter;
import com.qlmjx.sevalopos.event.PaystatrEvent;
import com.qlmjx.sevalopos.https.HttpAddress;
import com.qlmjx.sevalopos.https.RequestParams;
import com.qlmjx.sevalopos.utils.ConstantsUtils;
import com.qlmjx.sevalopos.utils.Screensaver;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;
import com.universal.frame.pulltorefresh.PullToRefreshBase;
import com.universal.frame.pulltorefresh.PullToRefreshScrollView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import okhttp3.Request;

import static com.universal.frame.generalutils.DateUtil.starmpToData;


/**
 * 退出登录
 * @author Administrator
 */
public class ComfirmPay2Dialog extends BaseDialog implements ResultCallBack , PullToRefreshBase.OnRefreshListener2{
	private NoScrollingListView nlv_shoplist;
	private TextView tv_js_num, tv_yj, tv_yfj, tv_name, tv_time, tv_yfk, tv_ts,tv_ts1,tv_close;
	private PullToRefreshScrollView pull_refresh_scrollview;
	private List<OrederBean> orderlist = new ArrayList<OrederBean>();
	private ImageView iv_erweima;
	private OrderPayAdapter orderPayAdapter;
	private LinearLayout ll_top1;
	private String order_id;
	private ErW erW;

	private Screensaver SaleScreensaver;
	private String number;
	private ErW erweima;
	public ComfirmPay2Dialog(Context context) {
		super(context);
		this.context = context;
	}

	public ComfirmPay2Dialog(Context context, String order_id, ErW erW, int theme, LeaveMyDialogListener listener) {
		super(context, theme,listener);
		this.context = context;
		this.listener = listener;
		this.order_id = order_id;
		this.erW = erW;


	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.item_paydialog);
		this.setCancelable(false);
		this.setCanceledOnTouchOutside(false);
		tv_close = (TextView) findViewById(R.id.tv_close);
		 nlv_shoplist = (NoScrollingListView) findViewById(R.id.nlv_shoplist);
		 iv_erweima = (ImageView) findViewById(R.id.iv_erweima);
		 ll_top1 = (LinearLayout) findViewById(R.id.ll_top1);
		 tv_name = (TextView) findViewById(R.id.tv_name);
		 tv_time = (TextView) findViewById(R.id.tv_time);
		 tv_js_num = (TextView) findViewById(R.id.tv_js_num);
		 tv_yj = (TextView) findViewById(R.id.tv_yj);
		 tv_yfj = (TextView) findViewById(R.id.tv_yfj);
		 tv_yfk = (TextView) findViewById(R.id.tv_yfk);
		 tv_ts = (TextView) findViewById(R.id.tv_ts);
		tv_ts1= findViewById(R.id.tv_ts1);
		 pull_refresh_scrollview = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
		tv_close.setOnClickListener(this);
		SaleScreensaver = new Screensaver(10000); //定时5秒
		SaleScreensaver.setOnTimeOutListener(new Screensaver.OnTimeOutListener() {
			@Override
			public void onTimeOut(Screensaver screensaver) {
				getStateInfo(number);
			}
		}); //监听
		initScrollView();
		getOrderInfo(order_id);
		getErWeiMaData(erW);
	}
	//查看订单支付状态
	public void getStateInfo(String businessNo) {
		String userID = SharedPreferencesUtil.getString(getContext(), ConstantsUtils.USER_ID);
		OKHttpUtils.postAsync(context
				, HttpAddress.SERVICE_PAYSTATE, userID
				, new RequestParams(context).getPayStateParams(businessNo)
				, this
				, false, 1);
	}
//

	//查看商品详情
	public void getOrderInfo(String keyid) {
		String userID = SharedPreferencesUtil.getString(getContext(), ConstantsUtils.USER_ID);
		OKHttpUtils.postAsync(context
				, HttpAddress.SERVICE_ODDERDETAIL, userID
				, new RequestParams(context).getOrderParams(keyid)
				, this
				, false, 3);
	}
	public void initScrollView() {
		pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setPullLabel(
						getContext().getResources().getString(
								R.string.pull_to_refresh_pull_label_up));
		pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setRefreshingLabel(
						getContext().getResources().getString(
								R.string.pull_to_refresh_refreshing_label));
		pull_refresh_scrollview.getLoadingLayoutProxy(false, true)
				.setReleaseLabel(
						getContext().getResources().getString(
								R.string.pull_to_refresh_release_label));

		pull_refresh_scrollview.setMode(PullToRefreshBase.Mode.DISABLED);//both  可以上拉、可以下拉
		pull_refresh_scrollview.setOnRefreshListener(this);
		orderPayAdapter = new OrderPayAdapter(getContext(), orderlist);
		nlv_shoplist.setAdapter(orderPayAdapter);

	}


	@Override
	public void onSuccess(String response, int flag) {
		try {
			if (flag == 1) {
				getStateData(response);
			}

			if (flag == 3) {
				getOrderData(response);
			}
		} catch (Exception e) {
			GeneralUtil.tryShow(getContext(), e);
		}
	}

	private void getStateData(String response) {
		String flag = JsonUtil.getString(response, "flag", "");
		if ("0".equals(flag)){
			SaleScreensaver.resetTime();
		}else {
			EventBus.getDefault().post(new PaystatrEvent(2));
			this.dismiss();
		}

	}

	private void getErWeiMaData(ErW E) {

		if ("1".equals(E.check)) {
			iv_erweima.setVisibility(View.VISIBLE);
			tv_ts.setVisibility(View.GONE);
			tv_ts1.setVisibility(View.VISIBLE);
			tv_close.setVisibility(View.VISIBLE);
			//下单时间
			number = E.number;

			tv_name.setText(E.commPartSale.get(0).saleCustName);
			tv_time.setText(starmpToData(E.date));
			tv_yj.setText("￥" + E.commPartSale.get(0).saleAmount);
			tv_yfj.setText("￥" + E.commPartSale.get(0).remainder);
			tv_yfk.setText("￥" + E.commPartSale.get(0).hasPaid);
			Glide.with(getContext()).load(erweima(E.image)).fitCenter().into(iv_erweima);
			getStateInfo(number);
			SaleScreensaver.start();
		} else {
			iv_erweima.setVisibility(View.GONE);
			tv_ts.setVisibility(View.VISIBLE);
			tv_ts1.setVisibility(View.GONE);
			tv_close.setVisibility(View.GONE);
			ToastUtil.showToast(getContext(), "生成二维码失败");
		}
	}

	private void getOrderData(String response) {
		ManageOrderInfo info = JSON.parseObject(response, ManageOrderInfo.class);
		if (info.list != null) {
			if (info.list.size() > 0) {
				ll_top1.setVisibility(View.VISIBLE);
				orderlist.clear();
				orderlist.addAll(info.list);
			} else {
				orderlist.clear();
				ll_top1.setVisibility(View.GONE);
			}
			orderPayAdapter.notifyDataSetChanged();

		}else {
			ll_top1.setVisibility(View.GONE);
			orderlist.clear();
			orderPayAdapter.notifyDataSetChanged();

		}
	}
	@Override
	public void onFailure(Request request, IOException e, int flag) {

	}
	private byte[] erweima(String base64) {
		Bitmap bitmap = null;
		byte[] bitmapArray;
		bitmapArray = Base64.decode(base64, Base64.DEFAULT);
		bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
		byte[] bytes = baos.toByteArray();
		return bytes;
	}

		@Override
	public void onPullDownToRefresh(PullToRefreshBase refreshView) {

	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase refreshView) {

	}

	@Override
	protected void onStop() {
		super.onStop();
		SaleScreensaver.stop();
	}
}